package com.fate.controller;

import com.fate.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("p/order")
@Api(tags = "支付管理接口")
@Slf4j
public class PayController {

    @Autowired
    private PayService payService;

    /**
     * 预先下单 创建二维码的方法
     */
    @PostMapping("pay")
    @ApiOperation("创建支付二维码")
    public ResponseEntity<String> getPayQrCode(@RequestBody Map<String,String> map){
        String prePay = payService.createPrePay(map);
        return ResponseEntity.ok(prePay);
    }

    /**
     * 记住放行
     * 支付宝回调你 肯定没有token
     */
    @PostMapping("payNotify")
    @ApiOperation("支付的回调")
    public ResponseEntity<String> payNotify(@RequestParam Map<String,String>map){
        //支付宝回到你
        String tradeStatus = map.get("trade_status");
        //1.先看map里面的状态
        if (StringUtils.hasText(tradeStatus)){
            if (tradeStatus.equals("TRADE_SUCCESS")){
                //2.验签是否是支付宝回调的 就要验签了
                Boolean flag = payService.resCheck(map);
                if (flag){
                    //3.修改订单状态 已经支付
                    payService.changeOrderStatus(map.get("out_trade_no"));
                    return ResponseEntity.ok("成功");
                }
                log.error("你小心点,我记住你的ip");
                return ResponseEntity.badRequest().body("你小心点,我记住你的ip");
            }
        }
        return ResponseEntity.ok("失败");
    }



    /**
     * 查询ali那边是否成功
     *
     * @param orderNum
     * @return
     */
    @PostMapping("queryAliOrderPay")
    Boolean queryAliOrderPay(@RequestBody String orderNum) {
        return payService.queryAliOrderPay(orderNum);
    }





























}
